# Copyright 2009 http://www.yuntien.com
# Licensed under the Apache License, Version 2.0

from ytblog.controller.common import *

class BlogAddController(BlogController):

  def render(self, context):
    path = os.path.join(os.path.dirname(__file__), ytblog.config.TEMPLATE_DIR + 'add.html')
    self.response.out.write(template.render(path, context))    
  
  #display add blog ui
  @authorize(Blog, authorization.OPERATION_ADD)
  def get(self):
    context = { }
    self.add_common_context(context)
    self.render(context)
    
  def fill_data(self, blog):
    blog.title = self.request.get('title')
    blog.content = self.request.get('content')
    blog.add_tags(self.request.get('tags'))

  # post a new blog
  @check_tag
  @authorize(Blog, authorization.OPERATION_ADD)
  def post(self):    
    blog = Blog(author=users.get_current_user())
    self.fill_data(blog)
    blog.put()

    self.redirect(ytblog.config.CONFIG['url_blog'] + '/' + str(blog.key().id()) + '/')    
